package com.ruoyi.factory.bom.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.factory.bom.domain.BomCost;
import com.ruoyi.factory.bom.domain.BomTreeSelect;
import com.ruoyi.factory.bom.service.IBomCostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * bom模型Controller
 *
 * @author ruoyi
 * @date 2022-10-30
 */
@RestController
@RequestMapping("/system/bomCost")
public class BomCostController extends BaseController
{
    @Autowired
    private IBomCostService bomCostService;

    private List<BomTreeSelect>  bomTreeSelects= new ArrayList<>();
    private List<BomCost>  bomTreeSelects2= new ArrayList<>();

    /**
     * 查询bom模型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:bomCost:list')")
//    @GetMapping("/list")
//    public AjaxResult list(BomCost bomCost)
//    {
////        if(bomTreeSelects.isEmpty()){
//            List<BomCost> list = bomCostService.selectBomCostList(bomCost);
////            bomTreeSelects = bomCostService.buildBomCostTreeSelect(list);
////        }
////        return AjaxResult.success(bomTreeSelects);
//        return AjaxResult.success(list);
//    }
//
//    @PreAuthorize("@ss.hasPermi('system:bomCost:list')")
//    @GetMapping("/list")
//    public AjaxResult list2(BomCost bomCost)
//    {
//
//        List<BomCost> list = bomCostService.selectBomCostList(bomCost);
//        bomTreeSelects = bomCostService.buildBomCostTreeSelect(list);
//
//        return AjaxResult.success(bomTreeSelects);
////        return AjaxResult.success(list);
//    }

    @PreAuthorize("@ss.hasPermi('system:bomCost:list')")
    @GetMapping("/list")
    public AjaxResult list3(BomCost bomCost)
    {
        if(bomTreeSelects2.isEmpty()){
            List<BomCost> list = bomCostService.selectBomCostList(bomCost);
            bomTreeSelects2 = bomCostService.buildBomCostTreeSelect2(list);
        }
        return AjaxResult.success(bomTreeSelects2);
//        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:bomCost:list')")
    @GetMapping("/getroot")
    public TableDataInfo getroot(BomCost bomCost)
    {
        startPage();
        List<BomCost> list = bomCostService.selectBomCostList(bomCost);
        return getDataTable(list);
//        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:bomCost:list')")
    @GetMapping("/getchildren")
    public AjaxResult getchildren(BomCost bomCost)
    {
        BomCost bomCost2 = new BomCost();
        BomCost bomCost1 = bomCostService.selectBomCostById(bomCost.getId());
        bomCost2.setRootCode(bomCost1.getRootCode());
        bomCost2.setBomVar(bomCost1.getBomVar());
        bomCost2.setParentId(bomCost1.getPartId());
        //查询出该父节点下的字节点
        List<BomCost> list = bomCostService.selectBomCostList(bomCost2);

        return AjaxResult.success(list);

    }


    /**
     * 获取bom下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BomCost bomCost)
    {
        List<BomCost> bomCosts = bomCostService.selectBomCostList(bomCost);
        return AjaxResult.success(bomCostService.buildBomCostTreeSelect(bomCosts));
    }

    /**
     * 导出bom模型列表
     */
    @PreAuthorize("@ss.hasPermi('system:bomCost:export')")
    @Log(title = "bom模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomCost bomCost)
    {
        List<BomCost> list = bomCostService.selectBomCostList(bomCost);
        ExcelUtil<BomCost> util = new ExcelUtil<BomCost>(BomCost.class);
        util.exportExcel(response, list, "bom模型数据");
    }

    /**
     * 获取bom模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bomCost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(bomCostService.selectBomCostById(id));
    }

    /**
     * 新增bom模型
     */
    @PreAuthorize("@ss.hasPermi('system:bomCost:add')")
    @Log(title = "bom模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomCost bomCost)
    {
        return toAjax(bomCostService.insertBomCost(bomCost));
    }

    /**
     * 修改bom模型
     */
    @PreAuthorize("@ss.hasPermi('system:bomCost:edit')")
    @Log(title = "bom模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomCost bomCost)
    {
        return toAjax(bomCostService.updateBomCost(bomCost));
    }

    /**
     * 删除bom模型
     */
    @PreAuthorize("@ss.hasPermi('system:bomCost:remove')")
    @Log(title = "bom模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bomCostService.deleteBomCostByIds(ids));
    }
}
